﻿using DDD.Grpc.Client.Test.Clients;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace DDD.Grpc.Client.Test
{
    public class HostedService : IHostedService
    {
        private readonly UserServiceClientTest _userServiceClientTest;
        private readonly ILogger<HostedService> _logger;

        public HostedService(UserServiceClientTest userServiceClientTest, ILogger<HostedService> logger)
        {
            _userServiceClientTest = userServiceClientTest;
            _logger = logger;
        }

        public async Task StartAsync(CancellationToken cancellationToken)
        {
            try
            {
                var result = await _userServiceClientTest.GetUserByIdAsync();

                Console.WriteLine("Grpc测试成功");
            }
            catch (Exception ex)
            {
                _logger.LogError($"Grpc测试失败,error:{ex.Message}");
            }
        }

        public Task StopAsync(CancellationToken cancellationToken)
        {
            return Task.CompletedTask;
        }
    }
}
